#include "DataStruct.fxh"
#include "States.fxh"
//float4x4 mWorld;                  // World matrix for object
float4x4 mWorldViewProjection;    // World * View * Projection matrix
float mHeightRange;               // Height value range
float4 vNormalDebugColor;
Texture1D texColorBar;         //1D texture for color bar

//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT_POSDEPTH RenderHeightMapVS( VS_INPUT_VERT inVert )
{
    VS_OUTPUT_POSDEPTH Output;
	     
    // Transform the position from object space to homogeneous projection space
	// Be careful!!! use float4 although the input is float3!!!
	float3 pos = inVert.Pos;
	//if(pos.z < 0)
		//pos.z = 0;
	Output.Position = mul(float4(pos,1.0), mWorldViewProjection);
	    
    // Just copy the texture coordinate through
    Output.Depth = inVert.Pos.z; 
    
    return Output;    
}


//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
float4 RenderHeightMapPS( VS_OUTPUT_POSDEPTH In ) : SV_TARGET
{ 
	if(In.Depth < 0)
		return float4(0,0,0,1);
	else
		{   
			float HeightNormalized = In.Depth / mHeightRange;
			return float4(HeightNormalized,HeightNormalized,HeightNormalized,1.0f);
			//return texColorBar.Sample(LinearSampler_Clamp, HeightNormalized);
		}
	//return float4(1.0f,1.0f,1.0f,1.0f);
}

//--------------------------------------------------------------------------------------
// Renders scene 
//--------------------------------------------------------------------------------------
technique11 RenderHeightMap
{
    pass P0
    {        
		SetRasterizerState(RenderMesh_RS);  
        SetVertexShader(CompileShader(vs_5_0, RenderHeightMapVS()));
		SetGeometryShader( NULL );
        SetPixelShader(CompileShader(ps_5_0, RenderHeightMapPS())); 
    }
}

technique11 RenderHeightMap_WIRE
{
    pass P0
    {        
		SetRasterizerState(RenderHM_RS);  
		SetVertexShader(CompileShader(vs_5_0, RenderHeightMapVS()));
		SetGeometryShader( NULL );
        SetPixelShader(CompileShader(ps_5_0, RenderHeightMapPS())); 
    }
}

//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT_VERT RenderHeightMapNormalVS( VS_INPUT_VERT inVert )
{
    VS_OUTPUT_VERT Output;
	     
    // Transform the position from object space to homogeneous projection space
	// Be careful!!! use float4 although the input is float3!!!
	float3 pos = inVert.Pos;
	Output.Position = mul(float4(pos,1.0), mWorldViewProjection);
	return Output;    
}


//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
float4 RenderHeightMapNormalPS( VS_OUTPUT_VERT In ) : SV_TARGET
{ 
	return vNormalDebugColor;
}

//--------------------------------------------------------------------------------------
// Renders normal line 
//--------------------------------------------------------------------------------------
technique11 RenderHeightMapNormal
{
    pass P0
    {        
		SetRasterizerState(RenderHM_RS);  
        SetVertexShader(CompileShader(vs_5_0, RenderHeightMapNormalVS()));
		SetGeometryShader( NULL );
        SetPixelShader(CompileShader(ps_5_0, RenderHeightMapNormalPS())); 
    }
}
